package com.qmth.gkwb.data.service;

import java.util.Date;
import java.util.List;





import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qmth.gkwb.data.constant.SignStatus;
import com.qmth.gkwb.data.document.Pay;
import com.qmth.gkwb.data.entity.BKGLMD;
import com.qmth.gkwb.data.document.Student;
import com.qmth.gkwb.data.exception.CertinoExistException;
import com.qmth.gkwb.data.exception.CommonBusinessException;
import com.qmth.gkwb.data.exception.OrderCreateFailException;
import com.qmth.gkwb.data.exception.SignForbiddenException;
import com.qmth.gkwb.data.pay.AmountService;
import com.qmth.gkwb.data.pay.OrderService;
import com.qmth.gkwb.data.repository.GLMDRepo;
import com.qmth.gkwb.data.repository.OrderRepo;
import com.qmth.gkwb.data.repository.StudentRepo;
import com.qmth.pay.exception.KeyErrorException;

@Service
public class StudentService {

	@Autowired private StudentRepo studentRepo;
	@Autowired private GLMDRepo glmdRepo;
	
	@Autowired private OrderService orderService;
	@Autowired private OrderRepo orderRepo;
	
	
	/**
	 * 报名信息提交,处理新报名和修改报名信息业务
	 * @param student
	 * @return
	 * @throws SignForbiddenException
	 * @throws CertinoExistException
	 * @throws OrderCreateFailException
	 * @throws KeyErrorException
	 * @throws CommonBusinessException
	 */
	@Transactional
	public Student apply(Student student) throws SignForbiddenException,CertinoExistException,OrderCreateFailException,KeyErrorException, CommonBusinessException{
		String id = student.getId();
		String certino = student.getCertino();
		//判断是否被拦截
		List<BKGLMD> glmd = glmdRepo.findByCertino(certino);
		if(glmd.size() > 0){
			throw new SignForbiddenException();
		}
		//判断是否是更新操作,如果是更新，同步到主数据库,但如果已缴费，修改了报考信息，则抛出异常
		if(id != null){
			Student source = studentRepo.findOne(id);
			
			//判断身份证号是否修改，如果修改，查询是否已存在
			if(!source.getCertino().equals(certino)){
				List<Student> list = studentRepo.findByCertino(certino);
				if(list.size() > 0){
					throw new CertinoExistException();
				}
			}
			//判断缴费和报考信息是否修改
			if(!student.getSignString().equals(source.getSignString())){
				boolean isPaid = orderService.isOrderPaid(student.getExamId());
				if(isPaid){
					throw new CommonBusinessException("已完成缴费，不允许修改报考信息!");
				}
			}
			//如果已经同步，修改isSyc为false
			if(student.getIsSyc()){
				student.setIsSyc(false);
			}
		}
		else{
			List<Student> list = studentRepo.findByCertino(certino);
			if(list.size() > 0){
				throw new CertinoExistException();
			}
			student.setIsReg(true);
			student.setSycFail(false);
			student.setRegDate(new Date());
		}
		//如果需要缴费，更新缴费信息
		double bill = student.getBill();
		String payContent = null;
		if(bill > 0 && student.getSubjectType() != null){
			if(student.getSubjectType().equals("SPORT") && student.getSportSigned() == 1){
				payContent = student.getSport().getName();
			}
			else if(student.getSubjectType().equals("ART")){
				payContent = "美术和设计学类";
			}
			if(student.getPay() == null){
				Pay pay = new Pay();
				pay.setContent(payContent);
				student.setPay(pay);
			}
			else{
				student.getPay().setContent(payContent);
			}
		}
		else if(bill == 0){
			student.setPay(null);
		}
		studentRepo.save(student);
		return student;
	}
	
	/**
	 * 获取考生报名状态<br />
	 * 判断是否需要缴费<br />
	 * 判断是否已经完成缴费<br />
	 * @param student
	 * @return
	 */
	public SignStatus getStudentStatus(Student student){
		SignStatus status = SignStatus.COMPLETE;
		boolean isNeedPay = false;
		if(student.getBill() > 0){
			isNeedPay = true;
		}
		if(isNeedPay){
			boolean isPaid = orderService.isOrderPaid(student.getExamId());
			if(!isPaid){
				status = SignStatus.PAYING;
			}
			else{
				status = SignStatus.PAID;
			}
		}
		return status;
	}
}
